import asyncio

# 基本用法
lock = asyncio.Lock()


async def task_with_lock(n):
    async with lock:
        # 此处代码同一时刻只能有一个协程执行
        print(f"task start {n}")
        await asyncio.sleep(1)
        print(f"task end {n}")


# 检查任务类型
async def main():
    tasks = [task_with_lock(_) for _ in range(3)]
    print([type(t) for t in tasks])  # 应看到 <class 'coroutine'>
    await asyncio.gather(*tasks)


# 运行主函数
asyncio.run(main())
